package com.ggh.jinjilive.ui.home.video;

import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.ViewGroup;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.lifecycle.Observer;
import androidx.lifecycle.ViewModelProvider;
import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.alibaba.android.arouter.launcher.ARouter;
import com.blankj.utilcode.util.SizeUtils;
import com.ggh.jinjilive.BaseApplication;
import com.ggh.jinjilive.R;
import com.ggh.jinjilive.databinding.FragmentVideoBinding;
import com.ggh.jinjilive.listener.AddItemOnClickListener;
import com.ggh.jinjilive.mobile.home.live.LiveCategory;
import com.ggh.jinjilive.mobile.home.video.VideoList;
import com.ggh.jinjilive.ui.BaseFragment;
import com.ggh.jinjilive.ui.home.video.adapter.VideoAdapter;
import com.ggh.jinjilive.ui.home.video.adapter.VideoTypeAdapter;
import com.ggh.jinjilive.utils.ARouterUtils;
import com.ggh.jinjilive.views.GridItemDecoration;
import com.google.gson.Gson;
import com.scwang.smart.refresh.footer.ClassicsFooter;
import com.scwang.smart.refresh.header.MaterialHeader;
import com.scwang.smart.refresh.layout.api.RefreshLayout;
import com.scwang.smart.refresh.layout.listener.OnLoadMoreListener;
import com.scwang.smart.refresh.layout.listener.OnRefreshListener;

import org.jetbrains.annotations.NotNull;

import java.util.List;

public class VideoFragment extends BaseFragment<FragmentVideoBinding> implements OnRefreshListener, OnLoadMoreListener {
    //Model
    private VideoViewModel videoViewModel;
    //适配器
    private VideoTypeAdapter videoTypeAdapter;
    //视频适配器
    private VideoAdapter videoAdapter;
    //
    private GridLayoutManager mCommodityManager;
    @Override
    public void onCreate(@Nullable @org.jetbrains.annotations.Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        BaseApplication.getInstance().getAppComponent().doInjectionMain(this);
        videoViewModel = new ViewModelProvider(this,viewModelFactory).get(VideoViewModel.class);
    }

    @Override
    public int initContentView(LayoutInflater inflater, @Nullable @org.jetbrains.annotations.Nullable ViewGroup container, @Nullable @org.jetbrains.annotations.Nullable Bundle savedInstanceState) {
        return R.layout.fragment_video;
    }

    @Override
    public void initView() {
        super.initView();
        binding.setVideoViewModel(videoViewModel);

        binding.refreshLayout.setRefreshHeader(new MaterialHeader(mContext));
        binding.refreshLayout.setRefreshFooter(new ClassicsFooter(mContext));

    }

    @Override
    public void initData() {
        super.initData();
        //类目
        videoTypeAdapter = new VideoTypeAdapter(mContext);
        LinearLayoutManager linearLayoutManager = new LinearLayoutManager(mContext);
        linearLayoutManager.setOrientation(RecyclerView.HORIZONTAL);
        binding.liveTypeList.setLayoutManager(linearLayoutManager);
        binding.liveTypeList.setAdapter(videoTypeAdapter);
        videoTypeAdapter.setAddItemOnClickListener(new AddItemOnClickListener() {
            @Override
            public void onSkipClicked(int position, Object mObject) {
                LiveCategory liveCategory = (LiveCategory) mObject;
                if(null != liveCategory){
                    videoViewModel.category_id.set(liveCategory.getId());
                    onRefresh(binding.refreshLayout);
                }
            }
        });
        //视频分类
        videoViewModel.VideoCategory();
        videoViewModel.listMutableLiveData.observe(this, new Observer<List<LiveCategory>>() {
            @Override
            public void onChanged(List<LiveCategory> liveCategories) {
                videoViewModel.category_id.set(liveCategories.get(0).getId());
                videoTypeAdapter.setAdd(liveCategories);
                onRefresh(binding.refreshLayout);
            }
        });
        //列表
        videoAdapter = new VideoAdapter(mContext);
        videoAdapter.setAddItemOnClickListener(new AddItemOnClickListener() {
            @Override
            public void onSkipClicked(int position, Object mObject) {
                List<VideoList> videoListList = (List<VideoList>) mObject;
                if(null != videoListList){
                    ARouter.getInstance().build(ARouterUtils.VideoPlayActivity)
                            .withInt("txlive_info_position",position)
                            .withString("videoListList",new Gson().toJson(videoListList)).navigation();
                }
            }
        });
        mCommodityManager = new GridLayoutManager(mContext, 2);
        binding.recyclerView.setLayoutManager(mCommodityManager);
        binding.recyclerView.addItemDecoration(new GridItemDecoration(2, SizeUtils.dp2px(10),false));
        binding.recyclerView.setAdapter(videoAdapter);
        videoViewModel.liveData.observe(this, new Observer<List<VideoList>>() {
            @Override
            public void onChanged(List<VideoList> videoLists) {
                binding.refreshLayout.finishRefresh();
                if(null != videoLists){
                    if(videoViewModel.isRefresh){
                        binding.refreshLayout.finishLoadMore();
                        videoAdapter.setAdd(videoLists);
                    }else {
                        if(videoLists.size() > 0){
                            videoAdapter.Add(videoLists);
                            binding.refreshLayout.finishLoadMore();
                        }else {
                            binding.refreshLayout.finishLoadMoreWithNoMoreData();
                        }
                    }
                }else {
                    binding.refreshLayout.finishLoadMore();
                }
            }
        });
    }


    @Override
    public void initViewObservable() {
        super.initViewObservable();
        //刷新
        binding.refreshLayout.setOnRefreshListener(this);
        //加载
        binding.refreshLayout.setOnLoadMoreListener(this);
    }

    @Override
    public void onRefresh(@NonNull @NotNull RefreshLayout refreshLayout) {
        videoViewModel.isRefresh = true;
        videoViewModel.videoList();
    }

    @Override
    public void onLoadMore(@NonNull @NotNull RefreshLayout refreshLayout) {
        videoViewModel.isRefresh = false;
        videoViewModel.videoList();
    }

}
